package com.googlecode.perfectable.visuable.parse;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.junit.Assert.assertFalse;

import java.util.Iterator;

import com.googlecode.perfectable.visuable.renderer.ElementRenderer;
import com.googlecode.perfectable.visuable.renderer.VisualRenderer;

final class StructureCheckingVisualRenderer implements VisualRenderer {

	private final Iterator<ElementStructure> structures;

	public StructureCheckingVisualRenderer(Iterable<ElementStructure> structures) {
		checkNotNull(structures);
		this.structures = structures.iterator();
	}

	@Override
	public ElementRenderer element(String elementName) {
		if(!this.structures.hasNext()) {
			throw new AssertionError("Unexpected element " + elementName);
		}
		ElementStructure structure = this.structures.next();
		structure.assertElementName(elementName);
		return new StructureCheckingElementRenderer(structure);
	}

	@Override
	public void close() {
		assertFalse(this.structures.hasNext());
	}

}
